#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include "ni488.h"

#define ARRAYSIZE 100            // Size of read buffer
int Dev; // Device handle
char ReadBuffer[ARRAYSIZE + 1]; // Read data buffer
char ErrorMnemonic[21][5] = { "EDVR", "ECIC", "ENOL", "EADR", "EARG", "ESAC",
		"EABO", "ENEB", "EDMA", "", "EOIP", "ECAP", "EFSO", "", "EBUS", "ESTB",
		"ESRQ", "", "", "", "ETAB" };
void GPIBCleanup(int Dev, char* ErrorMsg);

int _cdecl main(void)
{
	//  Dev = ibdev (BDINDEX, PRIMARY_ADDR_OF_DMM, NO_SECONDARY_ADDR, TIMEOUT, EOTMODE, EOSMODE);
	Dev = ibdev (0,11, 0, T10s, 1, 0); // 寻找器件
	if (ibsta & ERR)
	{
		GPIBCleanup(Dev, "Unable to open device");
		return 1;
	}

	ibclr (Dev); // 器件清零                           
	if (ibsta & ERR)
	{	GPIBCleanup(Dev, "Unable to clear device");
		return 1;
	}

	ibwrt (Dev, "*IDN?", 5L); //  与器件通信
	if (ibsta & ERR)
	{
		GPIBCleanup(Dev, "Unable to write to device");
		return 1;
	}
	ibrd (Dev, ReadBuffer, ARRAYSIZE);
	if (ibsta & ERR)
	{
		GPIBCleanup(Dev, "Unable to read data from device");
		return 1;
	}
	ReadBuffer[ibcntl] = '\0';
	printf("Returned string:  %s\n", ReadBuffer);

	ibonl (Dev, 0); //  器件挂起               
	return 0;
}

void GPIBCleanup(int ud, char* ErrorMsg) {
	printf("Error : %s\nibsta = 0x%x iberr = %d (%s)\n", ErrorMsg, ibsta,
			iberr, ErrorMnemonic[iberr]);
	if (ud != -1) {
		printf("Cleanup: Taking device offline\n");
		ibonl(ud, 0);
	}
}
